package soul.resource;

import soul.domain.code.Status;
import soul.domain.response.Response;
import soul.entity.Org;
import soul.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import soul.filter.AuthFilter;
import soul.service.OrgService;

import java.util.*;

/**
 * Created by admin on 2016/11/27.
 */
@Controller
@RequestMapping("/org")
public class OrgResource {

    @Autowired
    private OrgService orgService;

    @RequestMapping("/list")
    @ResponseBody
    public Response list() {
        User user = (User) AuthFilter.getVar("user");
        Assert.notNull(user, "没有权限");
        Assert.notNull(user.getType(), "没有权限");
        if (Objects.equals(user.getType(), User.TYPE_ORG)) {//组织管理员
            Map<String, Object> param = new HashMap<>();
            param.put("userId", user.getId());
            List<Org> orgs = orgService.select(param);
            return Response.ok(Status.Ok,orgs);
        } else if (user.getType().equals(User.TYPE_SUPPER)) {
            Map<String, Object> param = new HashMap<>();
            List<Org> orgs = orgService.select(param);
            return Response.ok(Status.Ok,orgs);
        }else {
            throw new RuntimeException("type不对");
        }
    }
}
